<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
	<script src="/js/lws-common.js"></script>
	<title></title>
	<!-- Bootstrap core CSS -->
	<link href="/css/bootstrap.min.css" rel="stylesheet">
	<!-- Bootstrap theme -->
	<link href="/css/bootstrap-theme.min.css" rel="stylesheet">
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<link href="/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
	<!-- Custom styles for this template -->
	<link href="/css/navbar.css" rel="stylesheet">

</head>

<body>

	<div class="container">

		<!-- Static navbar -->
		<nav class="navbar navbar-inverse">
			<div class="container-fluid">
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
						<span class="sr-only">Toggle navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand"><div class=camera>Yi Camera</div></a>
				</div>
				<div id="navbar" class="navbar-collapse collapse">
					<ul class="nav navbar-nav">
						<li class="active"><a href="proxy_config.html">ProxyChains-ng</a></li>
						<li><a href="config.html">System Config</a></li>
						<li><a href="about.html">About</a></li>
						<li><a href="reboot.html">Reboot</a></li>
					</ul>
				</div><!--/.nav-collapse -->
			</div><!--/.container-fluid -->
		</nav>

		<!-- Main component for a primary marketing message or call to action -->
		<div class="page-header">
			<h1>ProxyChains-ng Download Proxy List</h1>
		</div>

		<div class="progress">
			<div class="progress-bar" role="progressbar" style="width: 0%;"></div>
		</div>

		<div id="error"></div>
		<div id="warning"></div>
		<div id="information"></div>
		
		<table class="table table-bordered table-striped table-condensed">
			<thead>
				<tr>
					<th>#</th>
					<th>Proxy Server</th>
					<th>Country</th>
					<th>Proxy Connectivity</th>
					<th>Server Connectivity</th>
					<th> </th>
				</tr>
			</thead>
			<tbody id="test_list_table"></tbody>
		</table>
		<h4><strong>Log</strong></h4>
		<pre class="pre-scrollable small" id="PROXYCHAINSNG_log" name="log"></pre>
		<p>Service Provided by <a href="https://freevpn.ninja/">freevpn.ninja</a> and <a href="http://ipinfo.io">ipinfo.io</a>.</p>

		<div class="col-sm-12 button-wrapper">
			<a class="btn btn-primary" href="proxy_config.html" role="button">Back</a>
			<button type="button" class="btn btn-primary" id="stop">Stop</button>
			<button type="button" class="btn btn-primary" id="save_proxy_list">Save Selected Proxies</button>
		</div>
		<div class="col-sm-4">
			<div class="input-group">
				<div class="input-group-btn">
					<button type="button" id="download_num" class="btn btn-primary">Download Proxies</button>
					<button type="button" id="carat" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						<span class="caret"></span>
					</button>
					<ul class="dropdown-menu">
						<li><a id="download_num_socks">Download SOCKS Proxies</a></li>
						<li><a id="download_all">Download All Proxies</a></li>
						<li><a id="download_all_socks">Download All SOCKS Proxies</a></li>
					</ul>
				</div>
				<div>
					<input type="text" id="proxy_num" class="form-control" value="10"></input>
				</div>
			</div>
		</div>
	</div>

	<!-- Bootstrap core JavaScript
	================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	<!--<script>window.jQuery || document.write('<script src="js/vendor/jquery.min.js"><\/script>')</script>-->
	<script src="/js/bootstrap.min.js"></script>
	<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
	<script src="/js/ie10-viewport-bug-workaround.js"></script>

<script>
document.getElementById('download_num_socks').onclick = function(){download_num("SOCKS")};
document.getElementById('download_num').onclick = function(){download_num("ALL_PROXY")};
document.getElementById('download_all_socks').onclick = function(){download_all("SOCKS")};
document.getElementById('download_all').onclick = function(){download_all("ALL_PROXY")};
document.getElementById('stop').onclick = stop;
document.getElementById('save_proxy_list').onclick = save_proxy_list;
var info_num;

function get_appropriate_ws_url()
{
	var pcol;
	var u = document.URL;

	/*
	 * We open the websocket encrypted if this page came on an
	 * https:// url itself, otherwise unencrypted
	 */

	if (u.substring(0, 5) == "https") {
		pcol = "wss://";
		u = u.substr(8);
	} else {
		pcol = "ws://";
		if (u.substring(0, 4) == "http")
			u = u.substr(7);
	}

	u = u.split('/');

	/* + "/xxx" bit is for IE10 workaround */

	return pcol + u[0] + "/xxx";
}

/* yi-hack-v3_info protocol */
	
	var socket_info;

	if (typeof MozWebSocket != "undefined") {
		socket_info = new MozWebSocket(get_appropriate_ws_url(),
				   "yi-hack-v3_info");
	} else {
		socket_info = new WebSocket(get_appropriate_ws_url(),
				   "yi-hack-v3_info");
	}

	try {
		socket_info.onopen = function() {
			init();
		} 

		socket_info.onmessage = function got_packet(msg) {
			jso = JSON.parse(msg.data);

			if (jso.message == "SEND_CAM_INFO") {
				document.title = jso.camera;

				document.getElementsByClassName("camera")[0].textContent = jso.camera;
			} else if (jso.message == "SEND_NOTIFICATION") {
				var message;

				switch (jso.n_type) {
					case "0":
						message = "<div class=\"alert alert-danger\" role=\"alert\"><strong>ERROR:</strong> " + jso.n_message + "</div>";
						document.getElementById("error").innerHTML += message;
						break;
					case "1":
						message = "<div class=\"alert alert-warning\" role=\"alert\"><strong>WARNING:</strong> " + jso.n_message + "</div>";
						document.getElementById("error").innerHTML += message;
						break;
					case "2":
						message = "<div class=\"alert alert-success\" role=\"alert\" id=\"" + info_num + "\">" + jso.n_message + "</div>";
						document.getElementById("information").innerHTML += message;
						setTimeout(remove_information, 2000, info_num);
						info_num++;
						break;
				}
			}
		} 

		socket_info.onclose = function() {
			
		}
	} catch(exception) {
		alert('<p>Error' + exception);  
	}

function init() {
	socket_info.send("SEND_CAM_INFO\n");
}

function save_proxy_list() {
	var message;
	var proxy_line = document.getElementsByClassName("proxy_line");
	var first_time = true;

	for(var i = 0; i < proxy_line.length; i++)
	{
		if (document.getElementsByClassName("check").item(i).checked == true) {
			message = "SAVE_PROXYCHAINSNG_LIST\n";
			if(first_time == true) {
				message += "OPEN\n";
				first_time = false;
			} else {
				message += "APPEND\n";
			}
			message += proxy_line.item(i).textContent;
			socket_info.send(message);
		}
	}

	if (first_time == true) {
		message = "<div class=\"alert alert-info\" role=\"alert\"><strong>INFORMATION:</strong> At least one proxy server has to be selected to save.</div>";
		document.getElementById("information").innerHTML += message;
	} else {
		socket_info.send("SAVE_PROXYCHAINSNG_LIST\nCLOSE\n");
	}
}

/* yi-hack-v3_test_proxy protocol */

	var socket_test_proxy;

	if (typeof MozWebSocket != "undefined") {
		socket_test_proxy = new MozWebSocket(get_appropriate_ws_url(),
				   "yi-hack-v3_test_proxy");
	} else {
		socket_test_proxy = new WebSocket(get_appropriate_ws_url(),
				   "yi-hack-v3_test_proxy");
	}

	try {
		socket_test_proxy.onopen = function() {
			document.getElementById("stop").disabled = true;
			document.getElementById("save_proxy_list").disabled = true;
		} 

		socket_test_proxy.onmessage = function got_packet(msg) {
			jso = JSON.parse(msg.data);

			if (jso.message == "SEND_LOG") {
				switch (jso.l_type) {
					case "0":
						document.getElementById("PROXYCHAINSNG_log").innerHTML += "<span class=\"text-primary\">" + jso.l_message + "</span>";
						break;
					case "1":
						document.getElementById("PROXYCHAINSNG_log").innerHTML += "<span class=\"text-success\">" + jso.l_message + "</span>";
						break;
					case "2":
						document.getElementById("PROXYCHAINSNG_log").innerHTML += "<span class=\"text-danger\">" + jso.l_message + "</span>";
						break;
					case "3":
						document.getElementById("PROXYCHAINSNG_log").innerHTML += "<span class=\"text-primary\">" + jso.l_message + "</span>";
						break;
				}
			} else if (jso.message == "SEND_PROXY_INFO") {
				var proxy_index_name;
				var table_row;
				var check_result;
				var country_result;
				var proxy_result;
				if (jso.country) {
					country_result = jso.country;
				} else {
					country_result = "&#8213;";
				}
				if (parseInt(jso.return_value) == 0) {
					proxy_result = "<span class=\"label label-success\">\u2714</span>";
					check_result = true;
					
				} else {
					proxy_result = "<span class=\"label label-danger\">\u2718</span>";
					check_result = false;
				}
				proxy_index_name = "proxy_index_" + jso.index;
				table_row = document.getElementsByClassName(proxy_index_name)[0].parentElement.parentElement;
				table_row.getElementsByClassName("check")[0].checked = check_result;
				table_row.getElementsByClassName("country")[0].innerHTML = country_result;
				table_row.getElementsByClassName("proxy_" + jso.iteration)[0].innerHTML = proxy_result;
			} else if (jso.message == "SEND_PROXY_CONNECT") {
				var proxy_index_name;
				var table_row;
				var check_result;
				var connect_result;
				if (parseInt(jso.return_value) == 0) {
					connect_result = "<span class=\"label label-success\">\u2714</span>";
					check_result = true;
					
				} else {
					connect_result = "<span class=\"label label-danger\">\u2718</span>";
					check_result = false;
				}
				proxy_index_name = "proxy_index_" + jso.index;
				table_row = document.getElementsByClassName(proxy_index_name)[0].parentElement.parentElement;
				table_row.getElementsByClassName("check")[0].checked = check_result;
				table_row.getElementsByClassName("connect_" + jso.iteration)[0].innerHTML = connect_result;
			} else if (jso.message == "SEND_NOTIFICATION") {
				var message;

				switch (jso.n_type) {
					case "0":
						message = "<div class=\"alert alert-danger\" role=\"alert\"><strong>ERROR:</strong> " + jso.n_message + "</div>";
						document.getElementById("error").innerHTML += message;
						break;
					case "1":
						message = "<div class=\"alert alert-warning\" role=\"alert\"><strong>WARNING:</strong> " + jso.n_message + "</div>";
						document.getElementById("error").innerHTML += message;
						break;
					case "2":
						message = "<div class=\"alert alert-success\" role=\"alert\" id=\"" + info_num + "\">" + jso.n_message + "</div>";
						document.getElementById("information").innerHTML += message;
						setTimeout(remove_information, 2000, info_num);
						info_num++;

						document.getElementById("stop").disabled = true;
						document.getElementById("save_proxy_list").disabled = false;
						document.getElementById("download_num").disabled = false;
						document.getElementById("proxy_num").disabled = false;
						document.getElementById("carat").disabled = false;

						break;
				}
			} else if (jso.message == "SEND_PROGRESS") {
				document.getElementsByClassName("progress-bar")[0].setAttribute("style","width:" + jso.percentage + "%");
			} else if (jso.message == "SEND_LIST") {
				var table = document.getElementById("test_list_table");
				var row = table.insertRow(table.length);
				var cell1 = row.insertCell(0);
				var cell2 = row.insertCell(1);
				var cell3 = row.insertCell(2);
				var cell4 = row.insertCell(3);
				var cell5 = row.insertCell(4);
				var cell6 = row.insertCell(5);
				
				cell1.innerHTML = "<span class=proxy_index_" + jso.index + ">" + jso.index + "</span>";
				cell2.innerHTML = "<span class=proxy_line>" + jso.proxy + "</span>";
				cell3.innerHTML = "<span class=country>&#8213;</span>"
				cell4.innerHTML = "<span class=proxy_1>&#8213;</span> <span class=proxy_2>&#8213;</span> <span class=proxy_3>&#8213;</span>";
				cell5.innerHTML = "<span class=connect_1>&#8213;</span> <span class=connect_2>&#8213;</span> <span class=connect_3>&#8213;</span>";
				cell6.innerHTML = "<input type=\"checkbox\" value=\"\" class=check>"
			}
		} 

		socket_test_proxy.onclose = function(){
			
		}
	} catch(exception) {
		alert('<p>Error' + exception);  
	}

function isPositiveInteger(n) {
    return /^\+?\d+$/.test(n);
}

function init_test_proxy(type, num) {
	var element = document.getElementById("validation_error");

	if (element) {
		document.getElementById("proxy_num").parentElement.className = "";
		document.getElementById("proxy_num").parentElement.removeChild(element);
	}

	document.getElementsByClassName("progress-bar")[0].setAttribute("style","width:0%");
	document.getElementById("error").innerHTML = "";
	document.getElementById("test_list_table").innerHTML = "";
	document.getElementById("PROXYCHAINSNG_log").innerHTML = "";
	document.getElementById("stop").disabled = false;
	document.getElementById("save_proxy_list").disabled = true;
	document.getElementById("download_num").disabled = true;
	document.getElementById("proxy_num").disabled = true;
	document.getElementById("carat").disabled = true;
	document.getElementById("PROXYCHAINSNG_log").value = "";
	socket_test_proxy.send("DOWNLOAD_LIST\n" + type + "\n" + num + "\n");
}

function stop() {
	socket_test_proxy.send("TEST_STOP\n");
	document.getElementById("stop").disabled = true;
}

function download_all(type) {
	init_test_proxy(type, "ALL");
}

function download_num(type) {
	var value = document.getElementById("proxy_num").value;

	if (isPositiveInteger(value)) {
		init_test_proxy(type, value);
	} else {
		var element = document.getElementById("validation_error");

		if (!element) {
			document.getElementById("proxy_num").parentElement.className = "has-error";
			element = document.createElement("span");
			element.className = "form-control-feedback";
			element.id = "validation_error";
			element.textContent = "\u2718";
			document.getElementById("proxy_num").parentElement.appendChild(element);
		}
	}
}

function remove_information(x) {
	var element = document.getElementById(x);
	element.parentNode.removeChild(element);
}

</script>

</body>

</html>
